<?php


include("Libraries/Dal/DB_Queries.php");
include("Libraries/BusinessLogic/general.php");


function register_game($title,$release_date,$category)
{
	if(($title!="")&&($category!=""))
	{
		ob_start();
		echo $title;
		$len = ob_get_length();
		ob_end_clean();
		if($len<20)
		{
			$boolean=check_game_exist($title);
			if ($boolean == false)
			{
				$category_id=get_game_category_id($category);
				insert_into_game($title,$release_date,$category_id);
				return 1; //to paixnidi exei kataxorithei!!!
			}
			else
			{
				return 2; //to paixnidi einai idi kataxorimeno
			}
		}
		else 
			return 3; //polu megalo onoma
	}
	else
	{
		return 4;  //den exei ginei eisagogi tou titlou i tis katigorias tou paixnidiou
	}
	
}



//#####################################################################
//vasilis: den tin vrika pouthena giafto ftiaxnw mia prosorina.
//epistrefei ta 10 neotera paixnidia.

function show_10_latest_games() 
{
$result=show_games("release_date desc");  
return $result;
}

//#####################################################################



function show_top_ten()
//vasilis:  tin alaxa edelos giati ixe  sql query edo mesa
//epistrefei ta 10 paixnidia me tin megaliteri vathmologia taxinomimena prwta ana bathmologia kai epita ana titlo
{
$result=show_games("rate desc");  
return $result;
}


//#####################################################################

function show_games_from_category($category,$order_by,$rows)
{
	$cat_id=get_game_category_id($category);
	
	
	//klisi synartisis gia na brw ta apotelesmata
	$result=show_games_where("category",$cat_id,"title");
	
	//orismos metriti gia ton kathorismo tou arithmou twn apotelesmatwn basi twn $rows
	$counter=0;
	while($row=mysql_fetch_array($result) && $counter<$rows)
	{
	//emfanizei to periexomeno tis ekastote grammis
	echo $row["game_id"]. "-" .$row["title"]. "-" .$row["release_date"]. "-" .$row["category_id"];
	echo "</br>";
	$counter++; //molis o metritis ftasei to $rows tha stamatisei
	}
}


//#####################################################################
function search_game($keyword,$sort_by)
 {
	if(check_null($keyword)) //elegxei an den exei dothei pedio gia anazitisi
	 {
		$pin=array("status"=>statheres::EMPTY_FIELD,"value"=>null);
		return $pin;
	 }
	elseif(check_null($sort_by)) //elegxei an den exei dothei pedio gia taksinomisi
	 {
		$pin=array("status"=>statheres::SELECT_SORT_FIELD,"value"=>null);
		return $pin;
	 }
	else //se periptosi pou exoun dothei ola ta pedia
	 {
		$len = strlen($keyword);
		if($len<100) //elegxei to megethos tou keimenou pou edose o xristis
		 {
			$sorted_result=search_game_title($keyword,$sort_by);
			if($sorted_result) //elegxei an ektelestike i seasrch_game_title
			 {
				if(mysql_num_rows($sorted_result)>0) //elegxei an epistrefei apotelesmata
				 {
					$pin=array("status"=>statheres::OLA_OK,"value"=>$sorted_result);
					return $pin;
				 }
				else 
				 {
					$pin=array("status"=>statheres::WRONG_TITLE,"value"=>null);
					return $pin;
				 }
			 }
		 }
		else 
		 {
			$pin=array("status"=>statheres::LARGE_FIELD,"value"=>null);
			return $pin;
		 }
	 }
 }   
//#####################################################################

function get_game_details($game_id)
{

	/*returns an array with game information:
				title
				release_date
				category
				average rate of the game as  rate
				review
	p.s. Does not contain comments.
	*/
	$gameDetails=show_games_where("game_id",$game_id,"title");
	$gameDetails = mysql_fetch_array($gameDetails);
	return $gameDetails;
}
//#####################################################################

function show_games_by_category($category)
{
	$category_id=get_game_category_id($category);
	$result=search_game_category($category_id);
	if($result)
	 {
		$pin=array("status"=>statheres::OLA_OK,"value"=>$result);
		return $pin;
	 }
	else
	 {
		$pin=array("status"=>statheres::NO_RESULT,"value"=>null);
		return $pin;
	 }
}
?>